{extend name="iframe" /}
{block name="body"}
<form action="" id="addScholar" class="layui-form" method="post">
    <div class="layui-tab" lay-filter="tab-all">
        <div class="layui-form-item">
            <div class="layui-inline">
                查询学者
            </div>
            <div class="layui-inline">
                <select name="student" lay-search="" id="student" lay-filter="student" >
                    <option value="" selected disabled style="display: none;">请输入学者/手机</option>
                </select>
            </div>
            <div class="layui-inline">
                <a onclick="searchfunc()" class="layui-btn">搜索学者</a>
            </div>
        </div>
        <ul id="box">
            <!-- <li>
                <span style="margin-right: 10px;">学者姓名</span>
                <span>
                    <input type="checkbox" name="fusion_data_warehouse" lay-skin="primary" title="融合数据仓权限" value="0">
                    <input type="checkbox" name="academic_portrait" lay-skin="primary" title="学术画像权限" value="1">
                    <input type="checkbox" name="technology_situation_perceive" lay-skin="primary" title="科技态势感知权限" value="2">
                </span>
                <span style="margin-right: 10px">
                    <a onclick="" class="layui-btn-sm layui-btn">删除学者</a>
                </span>
            </li> -->
        </ul>
    </div>
</form>
<div class="layui-form-item" style="margin-top: 20px">
    <div class="layui-input-block">
        <button onclick="submitForm()" class="layui-btn" >保存</button>
    </div>
</div>
{/block}
{block name="js"}
    <script>
        var app ={
            data:[],
            list:{},
            name:'',
            historyList:{}
        }
        layui.use(['element','form'], function () {
            var $ = layui.jquery;
            var element = layui.element;
            var form = layui.form
            element.on('tab(tab-all)', function (data) {
                console.log("111")
            })
            form.on('select(student)', function (data) {
                {
                    if(!data.value)return
                    var arr = app.data.filter(item => {
                       return item.kid == data.value
                    })
                    if(arr.length){
                        layer.msg('已选择该学者，无需重复选择')
                        return
                    }
                    app.data.push(app.list[data.value])
                    setTemplate()
                    console.log('112')
                }
            });
            $('#addScholar').on("input", "input", function(e){
                //获取input输入的值
                app.name = $(this)[0].value;
                
            });
            // $('#addScholar').on("blur", "input", function(e){
            //     //获取input输入的值
            //     search()
                
            // });

        })
        function searchfunc(){
            $('#addScholar input').val('')
            $('#student').val('')
            form.render()
            $.ajax({
                url: '/admin/scholarmenuauth/searchScholar?name=' + app.name,
                type: 'post',
                dataType: 'json',
                success: function (result) {
                    var str = '<option value="" selected disabled style="display: none;">请输入学者/手机</option>'
                    if(result.code == 1){
                        var datas = result.data.list
                        app.list = {};
                        datas.forEach(item => {
                            app.list[item.kid] = item
                            app.historyList[item.kid] = item
                            str += `<option value="${item.kid}">${item.scholar_name}(${item.phone})</option>` 
                        })
                        $('#student').html(str)
                        
                        form.render()
                        setTimeout(() => {
                            $('.layui-form-select').addClass('layui-form-selected')
                        }, 100);
                        
                    }
                    
                },
                error: function (xhr, state, errorThrown) {
                    layer.msg(state + '：' + errorThrown);
                }
            });
        }
        function setTemplate(kid){
            var str = ''
            app.data.forEach(item => {
                str+=  `<li style="margin-bottom:10px">
                        <span style="margin-right: 10px;display:inline-block;width:55px">${app.historyList[item.kid].scholar_name}</span>
                        <span>
                            <input type="checkbox" checked id="fusion_data_warehouse${item.kid}" name="fusion_data_warehouse" lay-skin="primary" title="融合数据仓权限" value="1">
                            <input type="checkbox" checked id="academic_portrait${item.kid}" name="academic_portrait" lay-skin="primary" title="学术画像权限" value="1">
                            <input type="checkbox" checked id="technology_situation_perceive${item.kid}" name="technology_situation_perceive" lay-skin="primary" title="科技态势感知权限" value="1">
                        </span>
                        <span style="margin-right: 10px">
                            <a onclick="del(${item.kid})" data-id="${item.kid}" class="layui-btn-sm layui-btn">删除学者</a>
                        </span>
                    </li>`
            })
            $('#box').html(str)
            form.render()
        }
        function del(kid){
            let index = 0
            app.data.some((item,i) =>{
                if(item.kid == kid){
                    index = i
                    return true
                }
            })
            app.data.splice(index,1)
            setTemplate()
        }
        function submitForm(){
            let data = app.data;
            let params = []
            data.map(item => {
                let obj = {}
                if(item.kid){
                    obj.user_no = item.kid
                    obj.fusion_data_warehouse = $('#fusion_data_warehouse' + item.kid)[0].checked?1:0
                    obj.academic_portrait = $('#academic_portrait' + item.kid)[0].checked?1:0
                    obj.technology_situation_perceive = $('#technology_situation_perceive' + item.kid)[0].checked?1:0
                    params.push(obj)
                }
            })
            $.ajax({
                url: '/admin/scholarmenuauth/addScholarAuth',
                type: 'post',
                data:{
                    "list":JSON.stringify(params)
                },
                dataType: 'json',
                success: function (result) {
                    if(result.code == 1){
                        parent.location.href = '/admin/scholarmenuauth/index'
                    }
                    
                },
                error: function (xhr, state, errorThrown) {
                    layer.msg(state + '：' + errorThrown);
                }
            });
        }
        function rewardAndPunishDel(e) {
            $(e).parent().parent().remove();
        }

        laydate.render({
            elem: '#entrant_time',
            type: 'datetime'
        })
    </script>
{/block}
